package com.future.hedongjun.home.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.future.hedongjun.R;
import com.future.hedongjun.home.activity.ClassExerciseItemActivity;
import com.future.hedongjun.home.adapter.OptionsListAdapter;
import com.future.hedongjun.home.bean.QuestionBean;
import com.future.hedongjun.home.utils.NoScrollListview;


public class QuestionItemFragment extends Fragment {
    QuestionBean questionBean;
    int index;
    private OptionsListAdapter adapter;
    private StringBuffer sb;
    private NoScrollListview lv;
    LocalBroadcastManager mLocalBroadcastManager;
    ViewPager mViewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        View rootView = inflater.inflate(R.layout.question_pager_item,
                container, false);
        lv = (NoScrollListview) rootView.findViewById(R.id.lv_options);
        TextView tv_sequence = (TextView) rootView.findViewById(R.id.tv_Answer);
        TextView tv_total_count = (TextView) rootView.findViewById(R.id.tv_AllQuestion);
        TextView tv_description = (TextView) rootView.findViewById(R.id.tv_description);
        TextView tv_preious = (TextView) rootView.findViewById(R.id.tv_previous);
        TextView tv_next = (TextView) rootView.findViewById(R.id.tv_next);
        adapter = new OptionsListAdapter(getActivity(), questionBean.getQuestionOptions(), lv, index);
        lv.setAdapter(adapter);
        tv_sequence.setText("已答 " + (index + 1) + " 题");
        tv_total_count.setText("还剩 " + ClassExerciseItemActivity.questionlist.size() + " 题");
        tv_description.setText(questionBean.getDescription());

        //题干描述前面加上(单选题)或(多选题)
        //判断是单选还是多选
        int questionType = questionBean.getQuestionType();
        sb = new StringBuffer();
        if (questionType == 1) {//单选
            SpannableStringBuilder ssb = new SpannableStringBuilder("[单选题]");
            ssb.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(questionBean.getDescription());
            tv_description.setText(ssb);
            lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    adapter.notifyDataSetChanged();
                    //TODO 单选题选中后自动跳转到下一页
                    Intent intent = new Intent("com.leyikao.jumptonext");
                    mLocalBroadcastManager.sendBroadcast(intent);

                }
            });
//            btn_submit.setOnClickListener(new OnClickListener() {
//
//                @Override
//                public void onClick(View v) {
//                    long[] ids = lv.getCheckedItemIds();
//                    for (int i = 0; i < ids.length; i++) {
//                        long id = ids[i];
//                        sb.append(questionBean.getQuestionOptions().get((int) id).getName()).append(" ");
//                    }
//                    Toast.makeText(getActivity(), "选中的选项为" + sb.toString(), Toast.LENGTH_SHORT).show();
//                    sb.setLength(0);
//                }
//            });
            tv_preious.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    adapter.notifyDataSetChanged();
                    //TODO 单选题选中后自动跳转到下一页
                    //Intent intent = new Intent("com.leyikao.jumptopage");
                   // mLocalBroadcastManager.sendBroadcast(intent);
                    jumpToPre();
                }
            });

            tv_next.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    adapter.notifyDataSetChanged();
                    //TODO 单选题选中后自动跳转到下一页
                    //Intent intent = new Intent("com.leyikao.jumptonext");
                   // mLocalBroadcastManager.sendBroadcast(intent);
                    jumpToNext();
                }
            });
        } else if (questionType == 2) {//多选
            SpannableStringBuilder ssb = new SpannableStringBuilder("[多选题]");
            ssb.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(questionBean.getDescription());
            tv_description.setText(ssb);
            lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    adapter.notifyDataSetChanged();
                }
            });
//            btn_submit.setOnClickListener(new OnClickListener() {
//
//                @Override
//                public void onClick(View v) {
//                    long[] ids = lv.getCheckedItemIds();
//                    for (int i = 0; i < ids.length; i++) {
//                        long id = ids[i];
//                        sb.append(questionBean.getQuestionOptions().get((int) id).getName()).append(" ");
//                    }
//                    Toast.makeText(getActivity(), "选中的选项为" + sb.toString(), Toast.LENGTH_SHORT).show();
//                    sb.setLength(0);
//                }
//            });

            tv_preious.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    adapter.notifyDataSetChanged();
                    //TODO 单选题选中后自动跳转到下一页
                    //Intent intent = new Intent("com.leyikao.jumptopage");
                    // mLocalBroadcastManager.sendBroadcast(intent);
                    //jumpToPre();
                }
            });
            tv_next.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    adapter.notifyDataSetChanged();
                    //TODO 单选题选中后自动跳转到下一页
                   //Intent intent = new Intent("com.leyikao.jumptonext");
                   // mLocalBroadcastManager.sendBroadcast(intent);
                    jumpToNext();
                }
            });
        }

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void setList(QuestionBean list, int mIndex,ViewPager viewPager) {
        this.index = mIndex;
        this.questionBean = list;
        this.mViewPager=viewPager;
    }

    public void jumpToNext() {
        int position = mViewPager.getCurrentItem();
        mViewPager.setCurrentItem(position + 1);
    }

    public void jumpToPre() {
        int position = mViewPager.getCurrentItem();
        mViewPager.setCurrentItem(position - 1);
    }

}